package com.fay.mymiaosha.service.impl;

import com.fay.mymiaosha.dao.UserDoMapper;
import com.fay.mymiaosha.dao.UserPasswordDoMapper;
import com.fay.mymiaosha.dataobject.UserDo;
import com.fay.mymiaosha.dataobject.UserPasswordDo;
import com.fay.mymiaosha.exception.BizException;
import com.fay.mymiaosha.response.EmBizError;
import com.fay.mymiaosha.service.IUserService;
import com.fay.mymiaosha.service.model.UserModel;
import com.fay.mymiaosha.validator.ValidationResult;
import com.fay.mymiaosha.validator.ValidatorImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * @Title:
 * @Description:
 * @Author Fay Wang
 * @Date 2022/3/14 22:17
 * @Version 1.0
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserDoMapper userDoMapper;

    @Autowired
    private UserPasswordDoMapper userPasswordDoMapper;

    @Autowired
    private ValidatorImpl validator;

    @Override
    public UserModel getUserById(Integer id) {
        UserDo userDo = userDoMapper.selectByPrimaryKey(id);
        if (Objects.isNull(userDo)) {
            return null;
        }
        UserPasswordDo userPasswordDo = userPasswordDoMapper.selectByUserId(id);
        return convertFromDataObject(userDo, userPasswordDo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void register(UserModel userModel) throws BizException {
        if (Objects.isNull(userModel)) {
            throw new BizException(EmBizError.PARAMETER_VALIDATION_ERROR);
        }
        // arguments validation by hard coding
        /*if (StringUtils.isEmpty(userModel.getName())
                || userModel.getGender() == null
                || userModel.getAge() == null
                || StringUtils.isEmpty(userModel.getTelephone())) {}*/

        // arguments validation by validator
        ValidationResult result = validator.validate(userModel);

        if (result.isHasError()) {
            throw new BizException(EmBizError.PARAMETER_VALIDATION_ERROR, result.getErrMsg());
        }
        UserDo userDo = convertFromModel(userModel);
        userDoMapper.insertSelective(userDo);
        userModel.setId(userDo.getId());

        UserPasswordDo userPasswordDo =  convertPasswordFromModel(userModel);
        userPasswordDoMapper.insertSelective(userPasswordDo);
    }

    @Override
    public UserModel validateLogin(String telephone, String password) throws BizException {
        // get User_info by telephone
        UserDo userDo = userDoMapper.selectByTelephone(telephone);
        if (userDo == null) {
            throw new BizException(EmBizError.USER_LOGIN_FAIL);
        }
        UserPasswordDo userPasswordDo = userPasswordDoMapper.selectByUserId(userDo.getId());

        UserModel userModel = convertFromDataObject(userDo, userPasswordDo);
        // check password match or not
        if (!StringUtils.equals(password, userModel.getEncrptPassword())) {
            throw new BizException(EmBizError.USER_LOGIN_FAIL);
        }
        return userModel;
    }

    private UserPasswordDo convertPasswordFromModel(UserModel userModel) {
        if (userModel == null) {
            return null;
        }
        UserPasswordDo userPasswordDo = new UserPasswordDo();
        userPasswordDo.setEncrptPassword(userModel.getEncrptPassword());
        userPasswordDo.setUserId(userModel.getId());
        return userPasswordDo;
    }

    private UserModel convertFromDataObject(UserDo userDo, UserPasswordDo userPasswordDo) {
        UserModel userModel = new UserModel();
        BeanUtils.copyProperties(userDo, userModel);
        userModel.setEncrptPassword(userPasswordDo.getEncrptPassword());
        return userModel;
    }

    private UserDo convertFromModel(UserModel userModel) {
        if (userModel == null) {
            return null;
        }
        UserDo userDo = new UserDo();
        BeanUtils.copyProperties(userModel, userDo);
        return userDo;
    }

}
